package com.example.pc.adapterdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.pc.adapterdemo.constant.Constant;
import com.example.pc.adapterdemo.utils.GlideUtils;
import com.example.pc.adapterdemo.utils.JsonMapBean;
import com.example.pc.adapterdemo.utils.OkHttpUtils;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private JsonMapBean mJsonMapBean;
    private List<JsonMapBean> mDataList = new ArrayList<>();
    private BaseQuickAdapter<JsonMapBean,BaseViewHolder> mTestAdapter;
    private RefreshLayout mRefreshLayout;
    private int totalPages = 1;
    private int currentPage = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initData(currentPage);
        initView();
    }

    private void initView() {
        mRecyclerView = findViewById(R.id.mRecyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRefreshLayout=findViewById(R.id.mRefreshLayout);

        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(1000/*,false*/);//传入false表示刷新失败
                currentPage = 1;
                initData(currentPage);
            }
        });

        mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadMore(1000/*,false*/);//传入false表示加载失败
                initData(currentPage);
            }
        });
    }

    private void initData(final int page) {

        String url="project/list/"+page+"/json?cid=294";

        OkHttpUtils.get(Constant.HEADER_URL+url, new OkHttpUtils.OnOkHttpCallback() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onFailure(String msg) {

            }

            @Override
            public void onSuccessful(String json) {
                mJsonMapBean = JsonMapBean.fromJson(json);
                totalPages=mJsonMapBean.getBean("data").getInt("curPage");
                if(page>totalPages){
                    Toast.makeText(MainActivity.this, "数据加载完毕!", Toast.LENGTH_SHORT).show();
                }else {
                    if (currentPage == 1) {
                        mDataList.clear();
                    }
                    List<JsonMapBean> list = new ArrayList<>();
                    list = mJsonMapBean.getBean("data").getList("datas");
                    mDataList.addAll(list);
                    currentPage++;
                    if(currentPage == 2){
                        initAdapter();

                    }else{
                        mTestAdapter.notifyDataSetChanged();
                    }


                }
            }
        });
    }

    private void initAdapter() {
        mTestAdapter = new BaseQuickAdapter<JsonMapBean, BaseViewHolder>(R.layout.test_item_layout,mDataList) {
            @Override
            protected void convert(BaseViewHolder helper, JsonMapBean item) {
                helper.setText(R.id.mTitle,item.getString("title"));
                helper.setText(R.id.mContent,item.getString("desc"));
                GlideUtils.loadImageview(MainActivity.this,item.getString("envelopePic"), (ImageView) helper.getView(R.id.mImageView));

                //控件点击
                helper.addOnClickListener(R.id.mImageView);
            }
        };
        mRecyclerView.setAdapter(mTestAdapter);

        //列表点击
        mTestAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(MainActivity.this, "列表"+position, Toast.LENGTH_SHORT).show();
            }
        });

        //控件点击
        mTestAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()){
                    case R.id.mImageView:
                        Toast.makeText(MainActivity.this, "头像"+position, Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
    }
}
